<?php
class sanpham extends CI_Model{

    public function __construct()
    {
        // Call the Model constructor
        $this->load->database();
    }
    //Thông tin toàn b? s?n ph?m
    function get_num_product()
    {
        $query = $this->db->query('select count(id) as total
                                   from sanpham');
        $num = $query->row();
        return $num->total;
        
    }
    //Xây d?ng nav page d?a trên s?n ph?m  
	function get_product_paging($perPage,$offset)
	{
		//$offset = $this->getOffset();
		$query ="SELECT * 
                 FROM  sanpham,gia 
                 WHERE sanpham.id=gia.idSP 
                 Order By id Desc LIMIT $offset,$perPage";
		$q = $this->db->query($query);
		return $q->result();
	}
    //L?y t?t c?
    function getall($product_id)
    {
        $query = $this->db->query('SELECT *
                                   FROM sanpham');
        return $query->result();
    } 
    
    //L?y tên s?n ph?m
    function get_product_name($product_id)
    {
        $query = $this->db->query('SELECT TenSP
                                   FROM sanpham
                                   WHERE ID = '.$product_id);
        $name = $query->row();
        return $name->TenSP;
    }
    
    //L?y giá s?n ph?m
    function get_product_prices($product_id)
    {
        $query = $this->db->query('SELECT Gia
                                   FROM gia
                                   WHERE idSP = '.$product_id.' LIMIT 0,1');
        $price = $query->row();
        return $price->Gia;
    }
    
    //L?y thông tin s?n ph?m
    function get_product_info($id_product,$id_info_product_type)
    {
        $query = $this->db->query('SELECT ThongSo
                                   FROM thongsosp
                                   WHERE idSP = '.$id_product.
                                 ' AND idTS = '.$id_info_product_type);
        $info = $query->row();
        return $info->ThongSo;
    }
    
    //Thông s? s?n ph?m
    function get_type_kind()
    {
        $query = $this->db->query('SELECT id , Ten
                                   FROM thongso');
        return $query->result();
    }
    
    //S? lu?ng t?ng s?n ph?m t? 1 category cho tru?c
	function get_num_product_category($maincategory,$category)
	{
	    $parent = '1';
	   	switch($maincategory)
		{
		  case 'laptop_product':
            $parent = '1';
            break;
          case 'tablet_product':
            $parent = '2';
            break;
          case 'iphone_product':
            $parent ='3';
            break;	      
       }
		switch($category)
		{
			case 'all_product':
                if($this->construct_category_bit($parent) != '')
			         $strQuery = "SELECT count(id) as total
                            FROM  sanpham
                            WHERE idLoai in ".$this->construct_category_bit($parent);
                else
                    return 0;
			break;
			case 'hot_product':
             if($this->construct_category_bit($parent) != '')
		      $strQuery = "SELECT count(id) as total
                  FROM  sanpham
                  WHERE  hot=1 AND idLoai in ".$this->construct_category_bit($parent);
              else
                 return 0;
			break;
			case 'premium_product':
			  if($this->construct_category_bit($parent) != '')
		      $strQuery = "SELECT count(id) as total
                  FROM  sanpham
                  WHERE   caocap=1 AND  idLoai in ".$this->construct_category_bit($parent);
              else
                 return 0;
			break;           
		  }
          
             $query = $this->db->query($strQuery);
	         $num = $query->row();
             return $num->total;
         
    	}
    //L?y thông tin s?n ph?m theo category
	function get_product_category($maincategory,$category,$perPage,$offset)
	{
		$strQuery = "";
		 $parent = '1';
	   	switch($maincategory)
		{
		  case 'laptop_product':
            $parent = '1';
            break;
          case 'tablet_product':
            $parent = '2';
            break;
          case 'iphone_product':
            $parent ='3';
            break;	      
       }
		switch($category)
		{
			case 'all_product':
             if($this->construct_category_bit($parent) != '')
		      $strQuery = "SELECT * 
                  FROM  sanpham,gia 
                  WHERE sanpham.id=gia.idSP AND
				        idLoai in ".$this->construct_category_bit($parent).
                " Order By id";
              else
                 return null;
			break;
			case 'hot_product':
             if($this->construct_category_bit($parent) != '')
		      $strQuery = "SELECT * 
                  FROM  sanpham,gia 
                  WHERE sanpham.id=gia.idSP AND
				        idLoai in ".$this->construct_category_bit($parent).
                " AND hot=1 Order By id";
              else
                 return null;
            break;
			case 'premium_product':
			 if($this->construct_category_bit($parent) != '')
		      $strQuery = "SELECT * 
                  FROM  sanpham,gia 
                  WHERE sanpham.id=gia.idSP AND
				        idLoai in ".$this->construct_category_bit($parent).
                "  AND caocap=1 Order By id";
              else
                 return null;
			break;
			
		}
		$strQuery .= "  LIMIT $offset,$perPage"; 
		$query = $this->db->query($strQuery);
        return $query->result();
        

	} 
    function get_product_with_category($maincategory,$perPage,$offset)
	{
		$strQuery = "";
		 $parent = '1';
	   	switch($maincategory)
		{
		  case 'laptop_product':
            $parent = '1';
            break;
          case 'tablet_product':
            $parent = '2';
            break;
          case 'iphone_product':
            $parent ='3';
            break;	      
       }
	         if($this->construct_category_bit($parent) != '')
		     $strQuery = "SELECT * 
                  FROM  sanpham
                  WHERE sanpham.idLoai in ".$this->construct_category_bit($parent).
                  " Order By id";
    		  $strQuery .= "  LIMIT $offset,$perPage"; 
		      $query = $this->db->query($strQuery);
              return $query->result_array();
        

	} 
 function get_num_product_with_category($maincategory)
	{
		$strQuery = "";
		 $parent = '1';
	   	switch($maincategory)
		{
		  case 'laptop_product':
            $parent = '1';
            break;
          case 'tablet_product':
            $parent = '2';
            break;
          case 'iphone_product':
            $parent ='3';
            break;	      
       }
	         if($this->construct_category_bit($parent) != '')
		    { $strQuery = "SELECT COUNT(id) as total 
                  FROM  sanpham
                  WHERE sanpham.idLoai in ".$this->construct_category_bit($parent);
		      $query = $this->db->query($strQuery);
	          $num = $query->row();
              return $num->total;
              }
              return 0;
        

	} 
    //Tìm ki?m các lo?i s?n ph?m c?p du?i 
	function construct_category_bit($catid)
	{
	     $strQuery = "SELECT id FROM loai WHERE idLoai= '".$catid."' ";
		 $querys = $this->db->query($strQuery);
    	if($querys->num_rows>0)
        {
        	 $strstart = '(';
    		 $strend = ')';
    		 $result = $querys->result();
    		  foreach($result as $query)
              { 
    		
    		    $strstart .= $query->id.',';
              }
    		  return rtrim($strstart,',').$strend;
         }
         return ''; 
    
	}
     
    
    
    public function get_info_id($id)
    {
        $strQuery = 'SELECT id, Ten FROM thongso,thongsosp Where thongsosp.idTS=thongso.id and thongsosp.idsp='.$id;
        return $this->db->query($strQuery)->result();
    }
    
    public function get_detail($id)
    {
        $product_info = array();
        $strQuery = 'SELECT sanpham.*
                     FROM sanpham 
                     WHERE sanpham.id ='.$id;
        $result = $this->db->query($strQuery)->result_array();
        foreach($result[0] as $key=>$value)
        {
            $product_info[$key] = $value;
        }
        $product_info['price'] = $this->get_product_prices($id);
        $product_info['large_images'] = $this->get_images($id);
        $product_info['thumbnail_image'] = $this->get_images($id,true);
        $product_info['color'] = $this->get_color($id);
        $product_info['available'] = $this->get_available($id);
        $product_info['thongso'] = $this->get_thongso($id);
        return $product_info;
        
    }
    public function get_images($id,$thumbnail = false)
    {
        if(!$thumbnail)
        {
         $strQuery = 'SELECT hinhanh.DuongDan AS imageurl, hinhanh.MoTa AS decription,
                             anhlon.Rong AS width, anhlon.Cao AS height 
                      FROM anhlon,hinhanh
                      WHERE anhlon.idAnh = hinhanh.id
                      AND   anhlon.idSP = '.$id
                      ;
         return $this->db->query($strQuery)->result_array();
        }
        else
        {
            $strQuery = 'SELECT hinhanh.DuongDan AS thumbnail  
                      FROM thumbnail,hinhanh 
                      WHERE thumbnail.idAnh = hinhanh.id 
                      AND   thumbnail.idSP = '.$id
                      ;
            return $this->db->query($strQuery)->row()->thumbnail;   
        }
        
        
    }
    public function get_color($id)
    {
        $strQuery = 'SELECT mau.TenMau AS ten,mau.MaMau  AS ma
                      FROM mau 
                      WHERE mau.idSP = '.$id;
         return $this->db->query($strQuery)->result_array();
         
    }
    
    public function get_available($id)
    {
       $strQuery = 'SELECT NgayLen,NgayHetHan 
                     FROM lenke
                     WHERE lenke.idSP = ?';
        $result= $this->db->query($strQuery,array($id))->result_array();
        return true;
         
       
    }
    public function get_thongso($idSP)
    {
        
         $strQuery = 'SELECT thongso.Ten,thongsosp.ThongSo 
                     FROM thongsosp,thongso 
                     WHERE thongsosp.idTS = thongso.id AND 
                           thongsosp.idSP = '.$idSP;
         return $this->db->query($strQuery)->result_array();
 
    }
     public function get_str_thongso($idSP)
    {
        
         $strQuery = 'SELECT Concat(thongso.Ten," : ",thongsosp.ThongSo) as TS  
                     FROM thongsosp,thongso 
                     WHERE thongsosp.idTS = thongso.id AND
                           thongsosp.idSP = '.$idSP;
         return $this->db->query($strQuery)->result_array();
 
    }
    
   public function get_hot_product($num = 10)
   {
              $strQuery = "SELECT ID,TenSP  
                  FROM  sanpham
                  WHERE sanpham.hot =1  
                  Order By id";
    		  $strQuery .= "  LIMIT 0,$num"; 
		      $query = $this->db->query($strQuery);
              return $query->result_array();
   }
   // san pham tuong tu 
   public function get_like_product($idsp,$num = 5)
   {
	   $strQuery = "SELECT sanpham.id,TenSP
	   		FROM sanpham ,sanphamtuongtu
			WHERE sanpham.ID = sanphamtuongtu.idsp2 AND
				  sanphamtuongtu.idsp1 = $idsp";
	   $strQuery .= " LIMIT 0,$num";
	   $query = $this->db->query($strQuery);
	   $result= $query->result_array();
       return $result;
   }
   //show_orders
   public function get_orders($iddh)
   {
	   $strQuery = "SELECT TenSP,NguoiNhan,Email
	   			FROM sanpham,dathangsp,dondathang
				WHERE sanpham.id = dathangsp.idSP AND
					  dathangsp.idDonHang = dondathang.id AND
					  dondathang.id = $iddh " ;
	  return $this->db->query($strQuery)->result_array();		  
   }
   public function add_orders($uid,$nn,$em,$tt,$gc)
   {
	   $strQuery = "INSERT INTO dondathang (id,userID,NguoiNhan,Email,TrangThai,GhiChu)
                                   VALUE (NULL,$uid,$nn,$em,$tt,$gc) ";   
   }

}

?>